<html>
<head><meta charset="utf-8"><title>multiple borrows for nested mutable method calls · t-compiler/wg-polonius · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/186049-t-compiler/wg-polonius/index.html">t-compiler/wg-polonius</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/186049-t-compiler/wg-polonius/topic/multiple.20borrows.20for.20nested.20mutable.20method.20calls.html">multiple borrows for nested mutable method calls</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="186522425"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/186049-t-compiler/wg-polonius/topic/multiple%20borrows%20for%20nested%20mutable%20method%20calls/near/186522425" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/186049-t-compiler/wg-polonius/topic/multiple.20borrows.20for.20nested.20mutable.20method.20calls.html#186522425">(Jan 24 2020 at 18:58)</a>:</h4>
<div class="codehilite"><pre><span></span><span class="n">expr</span><span class="p">.</span><span class="n">mul_value</span><span class="p">(</span><span class="n">expr</span><span class="p">.</span><span class="n">signal</span><span class="p">(</span><span class="mi">1</span><span class="p">),</span><span class="w"> </span><span class="mf">6.0</span><span class="p">);</span><span class="w"></span>
</pre></div>


<div class="codehilite"><pre><span></span>cannot borrow `expr` as mutable more than once at a time
</pre></div>


<p>I know that NLL in general fixed this for outer mutable, inner immutable, but why doesn't it solve it for 2x mutable</p>



<a name="186522775"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/186049-t-compiler/wg-polonius/topic/multiple%20borrows%20for%20nested%20mutable%20method%20calls/near/186522775" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/186049-t-compiler/wg-polonius/topic/multiple.20borrows.20for.20nested.20mutable.20method.20calls.html#186522775">(Jan 24 2020 at 19:02)</a>:</h4>
<p>repro:</p>
<div class="codehilite"><pre><span></span><span class="k">struct</span> <span class="nc">Example</span><span class="p">;</span><span class="w"></span>

<span class="k">impl</span><span class="w"> </span><span class="n">Example</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">inner</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">)</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">outer</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">_</span>: <span class="p">())</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">ex</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Example</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="n">ex</span><span class="p">.</span><span class="n">outer</span><span class="p">(</span><span class="n">ex</span><span class="p">.</span><span class="n">inner</span><span class="p">());</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="188232907"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/186049-t-compiler/wg-polonius/topic/multiple%20borrows%20for%20nested%20mutable%20method%20calls/near/188232907" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/186049-t-compiler/wg-polonius/topic/multiple.20borrows.20for.20nested.20mutable.20method.20calls.html#188232907">(Feb 14 2020 at 18:37)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> the idea of 2PB (two-phase borrow) was that the outer <code>&amp;mut</code> is treated like an <code>&amp;</code> borrow until it is actualy used, <em>more or less</em></p>



<a name="188232916"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/186049-t-compiler/wg-polonius/topic/multiple%20borrows%20for%20nested%20mutable%20method%20calls/near/188232916" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/186049-t-compiler/wg-polonius/topic/multiple.20borrows.20for.20nested.20mutable.20method.20calls.html#188232916">(Feb 14 2020 at 18:37)</a>:</h4>
<p>this makes it compatible with an inner <code>&amp;</code></p>



<a name="188232927"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/186049-t-compiler/wg-polonius/topic/multiple%20borrows%20for%20nested%20mutable%20method%20calls/near/188232927" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/186049-t-compiler/wg-polonius/topic/multiple.20borrows.20for.20nested.20mutable.20method.20calls.html#188232927">(Feb 14 2020 at 18:37)</a>:</h4>
<p>because two <code>&amp;</code> mix</p>



<a name="188232931"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/186049-t-compiler/wg-polonius/topic/multiple%20borrows%20for%20nested%20mutable%20method%20calls/near/188232931" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/186049-t-compiler/wg-polonius/topic/multiple.20borrows.20for.20nested.20mutable.20method.20calls.html#188232931">(Feb 14 2020 at 18:37)</a>:</h4>
<p>but it is not compatible with an inner <code>&amp;mut</code></p>



<a name="188232994"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/186049-t-compiler/wg-polonius/topic/multiple%20borrows%20for%20nested%20mutable%20method%20calls/near/188232994" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/186049-t-compiler/wg-polonius/topic/multiple.20borrows.20for.20nested.20mutable.20method.20calls.html#188232994">(Feb 14 2020 at 18:38)</a>:</h4>
<p>if we wanted to support that, we'd have had to add a new kind of borrow -- i.e., an "unactivated" <code>&amp;mut</code> wouldn't act like an <code>&amp;</code>, it would act like something else (<code>&amp;const</code>, maybe... "somebody else can mutate")</p>



<a name="188232999"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/186049-t-compiler/wg-polonius/topic/multiple%20borrows%20for%20nested%20mutable%20method%20calls/near/188232999" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/186049-t-compiler/wg-polonius/topic/multiple.20borrows.20for.20nested.20mutable.20method.20calls.html#188232999">(Feb 14 2020 at 18:38)</a>:</h4>
<p>it's less clear that this is ok</p>



<a name="188233006"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/186049-t-compiler/wg-polonius/topic/multiple%20borrows%20for%20nested%20mutable%20method%20calls/near/188233006" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/186049-t-compiler/wg-polonius/topic/multiple.20borrows.20for.20nested.20mutable.20method.20calls.html#188233006">(Feb 14 2020 at 18:38)</a>:</h4>
<p>and it seemed to add more concepts</p>



<a name="188233029"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/186049-t-compiler/wg-polonius/topic/multiple%20borrows%20for%20nested%20mutable%20method%20calls/near/188233029" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/186049-t-compiler/wg-polonius/topic/multiple.20borrows.20for.20nested.20mutable.20method.20calls.html#188233029">(Feb 14 2020 at 18:38)</a>:</h4>
<p>so we opted not to support it</p>



<a name="188233032"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/186049-t-compiler/wg-polonius/topic/multiple%20borrows%20for%20nested%20mutable%20method%20calls/near/188233032" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/186049-t-compiler/wg-polonius/topic/multiple.20borrows.20for.20nested.20mutable.20method.20calls.html#188233032">(Feb 14 2020 at 18:38)</a>:</h4>
<p>but yeah sometimes it's annoying</p>



<a name="188233058"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/186049-t-compiler/wg-polonius/topic/multiple%20borrows%20for%20nested%20mutable%20method%20calls/near/188233058" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/186049-t-compiler/wg-polonius/topic/multiple.20borrows.20for.20nested.20mutable.20method.20calls.html#188233058">(Feb 14 2020 at 18:39)</a>:</h4>
<p>You know, I never actually thought about what "2 phase" actually meant, but that's a pretty clear description!</p>



<a name="188361515"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/186049-t-compiler/wg-polonius/topic/multiple%20borrows%20for%20nested%20mutable%20method%20calls/near/188361515" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/186049-t-compiler/wg-polonius/topic/multiple.20borrows.20for.20nested.20mutable.20method.20calls.html#188361515">(Feb 17 2020 at 08:58)</a>:</h4>
<p>(one of the nicest post about 2PB is <a href="https://smallcultfollowing.com/babysteps/blog/2017/03/01/nested-method-calls-via-two-phase-borrowing/" target="_blank" title="https://smallcultfollowing.com/babysteps/blog/2017/03/01/nested-method-calls-via-two-phase-borrowing/">https://smallcultfollowing.com/babysteps/blog/2017/03/01/nested-method-calls-via-two-phase-borrowing/</a> -- which is more descriptive than say <a href="https://rust-lang.github.io/rustc-guide/borrow_check/two_phase_borrows.html" target="_blank" title="https://rust-lang.github.io/rustc-guide/borrow_check/two_phase_borrows.html">https://rust-lang.github.io/rustc-guide/borrow_check/two_phase_borrows.html</a> -- even though it's still unclear to me how close/far the actual implementation ended up being in practice, especially related to the stacked borrows model and <a href="https://github.com/rust-lang/rust/issues/59159" target="_blank" title="https://github.com/rust-lang/rust/issues/59159">#59159</a>)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>